package com.nineclock.doc.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.db.sql.Query;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nineclock.common.entity.Result;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.threadlocal.CurrentUserHolder;
import com.nineclock.doc.dto.DocFileHistoryDto;
import com.nineclock.doc.mapper.DocFileMapper;
import com.nineclock.doc.mapper.DocHistoryMapper;
import com.nineclock.doc.pojo.DocFileHistory;
import com.nineclock.doc.service.DocHistoryService;
import com.nineclock.system.dto.SysCompanyUserDTO;
import com.nineclock.system.feign.SysUserFeign;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DocHistoryServiceImpl implements DocHistoryService {


    @Autowired
    private DocHistoryMapper docHistoryMapper;

    @Autowired
    private SysUserFeign userFeign;



    @Override
    public List<DocFileHistoryDto> seeDocEditHistory(Long fileId) {
         //健壮性判断
        if(fileId==null){
         throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }
        //根据文件id查询信息
        LambdaQueryWrapper<DocFileHistory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DocFileHistory::getFileId,fileId);
        //查到了对文件操作历史记录的集合
        List<DocFileHistory> docFileHistories = docHistoryMapper.selectList(wrapper);
        //创建一个封装对象的DocFileHistoryDto集合.
        ArrayList<DocFileHistoryDto> historyDtoArrayList = new ArrayList<>();
        //调用系统微服务获取当前企业的相关用户信息.
        List<SysCompanyUserDTO> data = userFeign.queryCompanyUser(null, CurrentUserHolder.get().getCompanyId()).getData();
        //遍历集合,取出其中的每一个对该文件进行操作过的历史记录对象
        for (DocFileHistory docFileHistory : docFileHistories) {
            //创建需要返回前段的DTO对象,把数据库查到的docFileHistory数据封装到DTO对象中
            DocFileHistoryDto docFileHistoryDto = new DocFileHistoryDto();
            docFileHistoryDto.setFileName(docFileHistory.getFileName());

            docFileHistoryDto.setId(docFileHistory.getId());

            docFileHistoryDto.setFileId(docFileHistory.getFileId());

            docFileHistoryDto.setContent(docFileHistory.getContent());

            docFileHistoryDto.setOperateTime(new DateTime());

            docFileHistoryDto.setIsCreate(docFileHistory.getIsCreate());

            docFileHistoryDto.setOperateUserId(docFileHistory.getOperateUserId());
            //调用系统为服务查询当前登录该企业的CompanyUser对象的集合.

            //遍历集合,companyuser对象中id和docFileHistory对象中的getOperateUserId相等,那么把调用查到的用户名赋值给docFileHistoryDto对象
            for (SysCompanyUserDTO companyUserDTO : data) {
                 if(companyUserDTO.getId()==docFileHistory.getOperateUserId()){
                   docFileHistoryDto.setUserName(companyUserDTO.getUserName());
                 }
            }
          //把docFileHistoryDto封装到historyDtoArrayList对象中.
         historyDtoArrayList.add(docFileHistoryDto);
        }
        //返回给前端查到的历史操作记录集合
        return historyDtoArrayList;
    }
}
